package com.orhon.smartcampus.modules.base.fetcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.GradesDao;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Periods;
import com.orhon.smartcampus.modules.base.entity.Subjects;
import com.orhon.smartcampus.modules.base.gqlModel.GQLGrades;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPeriod;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSubject;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.GradeMapper;
import com.orhon.smartcampus.modules.base.mm.PeriodMapper;
import com.orhon.smartcampus.modules.base.mm.SubjectMapper;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.student.gqlModel.GQLEclass;
import com.orhon.smartcampus.modules.student.mm.EclassMapper;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacher;
import com.orhon.smartcampus.modules.teacher.mm.TeacherMapper;
import com.orhon.smartcampus.utils.LangUtil;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class GradesFetcher implements DataFetcher {

    @Autowired
    GradesDao gradesDao;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        List<GQLGrades> GQLGrades = new ArrayList<>();
        GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
        
        String hql = "from Grades where deleted_at = null ";
        if(env.containsArgument("condition")) {
            HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
            if(map.get("id")!=null) {
                hql += "and id" + Util.decide(map.get("id"));
            }
            if(map.get("period")!=null) {
            	String ids = map.get("period").toString();
                hql += "and period_id in ("+ids.substring(1, ids.length()-1)+") ";
            }
            if(map.get("name")!=null){
                hql += LangUtil.decide("gradeName",map.get("name"));
            }
        }
        List<Grades> list = gradesDao.find(hql,null,page.getPage(),page.getLimit());

        for (Grades item : list) {

        	GQLPeriod gqlPeriod = null;
            if (env.getSelectionSet().contains("data/periods")){
                Periods periods = item.getPeriods();
                gqlPeriod = PeriodMapper.MAPPER.toPeriods(periods);
            }

           List<GQLTeacher> gqlTeacher = null;
           if (env.getSelectionSet().contains("data/teacher")){
               List<TInformation> tInformation = item.getTeacher();
               gqlTeacher = TeacherMapper.MAPPER.toTeacherList(tInformation);
           }

           List<GQLSubject> gqlSubject = null;
           if (env.getSelectionSet().contains("data/subjects")){
               List<Subjects> subjects = item.getSubjects();
               gqlSubject = SubjectMapper.MAPPER.toSubjectList(subjects);
           }

           List<GQLEclass> gqlEclasses = null;
           if (env.getSelectionSet().contains("data/eclasses")){
               List<Eclass> db_eclasses = item.getEclasses();
               gqlEclasses = EclassMapper.MAPPER.toEclassList(db_eclasses);
           }
 
 
           GQLGrades grades = GradeMapper.MAPPER.toGrade(item);
           grades.setPeriods(gqlPeriod);
           grades.setTeacher(gqlTeacher);
           grades.setSubjects(gqlSubject);
           grades.setEclasses(gqlEclasses);
           GQLGrades.add(grades);
		}
       
        
        Long total = 0L;
        if (env.getSelectionSet().contains("total")){
        	total = gradesDao.getTotalCount(hql);
        }
        return Result.ok().put("data" , GQLGrades).put("total" , total);
    }


}
